using UnityEngine;
using System.Collections;

public class FadeObject : MonoBehaviour 
{
	public FadeStyle fadeStyle;
	public GameObject fadeObject;
	public float timeEffect;
	public float amount;
	private TurnFade turnFade = new TurnFade();

	IEnumerator Start()
	{
		yield return new WaitForSeconds(3);

		switch(fadeStyle)
		{
			case FadeStyle.FLASHING:
				StartCoroutine(FlashingEffect());
				break;
			
			case FadeStyle.INTERPOLATE:
				turnFade.initialize(TurnFade.state.FADEIN,amount,fadeObject);
				StartCoroutine(InterpolateEffect());
				break;
		}
	}
	
	IEnumerator InterpolateEffect()
	{
		while(true)
		{
			turnFade.execute();
			
			yield return null;
		}
	}
	
	IEnumerator FlashingEffect()
	{
		yield return new WaitForSeconds(timeEffect);
		
		if(fadeObject.renderer.material.color.a == 0)
		{
			Color c = fadeObject.renderer.material.color;
			fadeObject.renderer.material.color = new Color(c.r,c.g,c.b,1);
		}
		else
		{
			Color c = fadeObject.renderer.material.color;
			fadeObject.renderer.material.color = new Color(c.r,c.g,c.b,0);
		}
				
		StartCoroutine(FlashingEffect());
	}
	
	public enum FadeStyle
	{
		FLASHING,
		INTERPOLATE
	}
}
